package com.stadium.web.domain;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fastsite.utils.DateUtils;
import com.fastsite.utils.StringUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 场馆对象 venue
 * 
 * @author ruoyi
 * @date 2020-03-15
 */
@ApiModel("场馆实体类")
@Table(name="venue")
@Data
@Entity
@EntityListeners(AuditingEntityListener.class)
public class Venue implements Serializable
{
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    /** ID */
    @Column(name = "ID")
    @ApiModelProperty("ID")
    private Long id;
    /** 场馆名称 */
    @Column(name = "USER_ID")
    @ApiModelProperty("所属用户")
    private Long userId;

    @OneToMany(fetch = FetchType.EAGER)
    @JoinColumn(name = "VENUE_ID",insertable = false,updatable = false)
    private List<Reserve> reserves;

    /** 场馆名称 */
    @Column(name = "NAME")
    @ApiModelProperty("场馆名称")
    private String name;
    /** 场馆名称 */
    @Column(name = "LOGO")
    @ApiModelProperty("场馆logo")
    private String logo;
    /** 是否预定 0:预定,1:未预定· */
    @Transient
    @ApiModelProperty("是否预定 0:预定,1:未预定·")
    private Boolean isReserve;

    /** 所属区域 */
    @Column(name = "REGION_ID")
    @ApiModelProperty("所属区域")
    private Long regionId;

    /** 场地费用 */
    @Column(name = "COST")
    @ApiModelProperty("场地费用")
    private BigDecimal cost;

    @JsonFormat(pattern="HH:mm")
    @Column(name = "FROM_TIME")
    @ApiModelProperty("开始营业时间")
    private String fromTime;

    @JsonFormat(pattern="HH:mm")
    @Column(name = "TO_TIME")
    @ApiModelProperty("结束营业时间")
    private String toTime;

    /** 创建时间 */
    @OrderBy("desc")
    @JsonFormat(pattern="yyyy-MM-dd  HH:mm")
        @Column(name = "CREATED_TIME")
    @ApiModelProperty("创建时间")
//    @CreatedDate
    private Date createdTime;

    @LastModifiedDate
    /** 更新时间 */
    @OrderBy("desc")
    @JsonFormat(pattern="yyyy-MM-dd  HH:mm")
        @Column(name = "UPDATED_TIME")
    @ApiModelProperty("更新时间")
    private Date updatedTime;

    @OneToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "REGION_ID",insertable = false,updatable = false)
    private Region region;

    private Byte status;

    public void setId(Long id)
    {
        this.id = id;
    }

    //获取预定信息
    public Boolean getIsReserve() {
        if(StringUtils.isNull(reserves)||reserves.size()==0){
            return false;
        }
        Date sysDate = DateUtils.getNowDate();
        Calendar increase=Calendar.getInstance();
        increase.setTime(sysDate);
        increase.add(Calendar.HOUR_OF_DAY, 3);
        Calendar subtract=Calendar.getInstance();
        subtract.setTime(sysDate);
        subtract.add(Calendar.HOUR_OF_DAY, -3);
        Date reserveTime =  reserves.get(0).getReserveTime();
        if(subtract.getTime().compareTo(reserveTime)>0&&increase.getTime().compareTo(reserveTime)<0){
            return false;
        }
        return true;
    }

    public Long getId()
    {
        return id;
    }
    public void setName(String name) 
    {
        this.name = name;
    }

    public String getName() 
    {
        return name;
    }
    public void setRegionId(Long regionId) 
    {
        this.regionId = regionId;
    }

    public void setCreatedTime(Date createdTime) 
    {
        this.createdTime = createdTime;
    }

    public Date getCreatedTime() 
    {
        return createdTime;
    }
    public void setUpdatedTime(Date updatedTime) 
    {
        this.updatedTime = updatedTime;
    }

    public Date getUpdatedTime() 
    {
        return updatedTime;
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
            .append("id", getId())
            .append("name", getName())
            .append("isReserve", getIsReserve())
            .append("regionId", getRegionId())
            .append("cost", getCost())
            .append("createdTime", getCreatedTime())
            .append("updatedTime", getUpdatedTime())
            .toString();
    }
}
